package com.jader.activities;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.beans.LunTan_huati_Bens;
import com.jader.main.MainActivity;
import com.jader.util.JaderApplication;
import com.jader.util.TiShi;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class HuaTiDetail extends Activity implements OnClickListener {
	LunTan_huati_Bens all_Bens;
	ImageView userNameHeardImageView, // 頭像
			fenxiangImageView;// 分享

	ImageView zanshiImageView1, zanshiImageView2, zanshiImageView3;

	TextView userNameTextView, // 發帖人姓名
			fatietimeTextView, // 發帖時間
			fatiethemTextView, // 发帖主题
			fatieneirongtTextView, // 發帖內容

			fanhuihuati;// 返回论坛
	BitmapUtils bitmapUtils;
	boolean scfalg;// 是否被收藏
	ImageView shoucang;// 收藏
	LunTan_huati_Bens luntan_huati;
	private String url1 = JaderApplication.getIP() + "/Jader/SerCollect";
	private String url = JaderApplication.getIP();
	private List<String> list_url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		setContentView(R.layout.activity_hua_ti_detail);
		bitmapUtils = JaderApplication.getBitmapUtils();
		Intent intent = this.getIntent();
		luntan_huati = (LunTan_huati_Bens) intent
				.getSerializableExtra("position");
		initView();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub

	}

	private void initView() {
		// TODO Auto-generated method stub
		all_Bens = (LunTan_huati_Bens) getIntent().getSerializableExtra(
				"position");
		fanhuihuati = (TextView) findViewById(R.id.fanhuihuati);
		userNameHeardImageView = (ImageView) findViewById(R.id.item_luntan_huati_detail_imageview);
		// zanImageView = (ImageView) findViewById(R.id.jianding_detaile_zan);
		fenxiangImageView = (ImageView) findViewById(R.id.huati_detail_fenxiang);
		shoucang = (ImageView) findViewById(R.id.huati_detail_shoucang);
		// huifuImageView = (ImageView)
		// findViewById(R.id.jianding_deatile_pinglun);

		zanshiImageView1 = (ImageView) findViewById(R.id.item_luntan_huati_detail_zhanshi);
		zanshiImageView2 = (ImageView) findViewById(R.id.item_luntan_huati_detail_zhanshi1);
		zanshiImageView3 = (ImageView) findViewById(R.id.item_luntan_huati_detail_zhanshi2);

		userNameTextView = (TextView) findViewById(R.id.item_luntan_huati_detail_nameText);
		fatietimeTextView = (TextView) findViewById(R.id.item_luntan_huati_detail_timeText);
		fatiethemTextView = (TextView) findViewById(R.id.detaile_biaoti);
		fatieneirongtTextView = (TextView) findViewById(R.id.detaile_neirong);

		zanshiImageView1.setOnClickListener(this);
		zanshiImageView2.setOnClickListener(this);
		zanshiImageView3.setOnClickListener(this);
		fenxiangImageView.setOnClickListener(this);
		bitmapUtils.display(userNameHeardImageView, JaderApplication.getIP()+"/Jader"+all_Bens.getUserBens()
				.getPicture());
		list_url = luntan_huati.getList();

		if (list_url == null || list_url.size() == 0) {
		}

		if (list_url != null && list_url.size() == 1) {
			// url1 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(0);
			bitmapUtils.display(zanshiImageView1, url + list_url.get(0));
		}
		if (list_url != null && list_url.size() == 2) {
			// url1 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(0);
			// url2 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(1);
			bitmapUtils.display(zanshiImageView1, url + list_url.get(0));
			bitmapUtils.display(zanshiImageView2, url + list_url.get(1));
		}

		if (list_url != null && list_url.size() > 2) {
			// url1 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(0);
			// url2 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(1);
			// url3 = "http://" + MyApplication.getIP() + ":8080/Third/image/" +
			// list_url.get(2);
			bitmapUtils.display(zanshiImageView1, url + list_url.get(0));
			bitmapUtils.display(zanshiImageView2, url + list_url.get(1));
			bitmapUtils.display(zanshiImageView3, url + list_url.get(2));
		}

		fanhuihuati.setOnClickListener(this);
		shoucang.setOnClickListener(this);
		userNameTextView.setText(all_Bens.getUserBens().getUsername());
		fatietimeTextView.setText(all_Bens.getDate());

		fatiethemTextView.setText(all_Bens.getTheme());
		fatieneirongtTextView.setText(all_Bens.getContent());
		getwebcheckshoucang(url1);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fanhuihuati:
			Intent intent = new Intent(HuaTiDetail.this, MainActivity.class);
			intent.putExtra("tiaozhuan", 3);
			startActivity(intent);
			break;
		// 收藏
		case R.id.huati_detail_shoucang:
			if (scfalg) {

				getwebshoucang(url1);
				shoucang.setImageResource(R.drawable.shoucang1);

				scfalg = false;
			} else {
				getwebquxiao(url1);
				shoucang.setImageResource(R.drawable.shoucang);

				scfalg = true;
			}

			break;
		case R.id.item_luntan_huati_detail_zhanshi:
			tiaoimage();
			break;
		case R.id.item_luntan_huati_detail_zhanshi1:
			tiaoimage();
			break;
		case R.id.item_luntan_huati_detail_zhanshi2:
			tiaoimage();
			break;
		case R.id.huati_detail_fenxiang:
			JaderApplication.startShareApp(HuaTiDetail.this, "分享到",
					all_Bens.getTheme(), all_Bens.getContent());
		default:
			break;
		}
	}

	private void tiaoimage() {
		// 定义一个数组，存放图片路径

		if (list_url.size() != 0) {
			// 当图片路径不为0时，传给数组
			String[] images = new String[list_url.size()];
			for (int i = 0; i < list_url.size(); i++) {
				images[i] = JaderApplication.getIP() + list_url.get(i);
			}
			Intent intent = new Intent(this, ImagePagerActivity.class);
			// 图片url,一般从数据库中或网络中获取
			intent.putExtra("images", images);
			this.startActivity(intent);
		} else if (list_url.size() == 0) {
			zanshiImageView1.setVisibility(View.GONE);
			zanshiImageView2.setVisibility(View.GONE);
			zanshiImageView3.setVisibility(View.GONE);
		}
	}

	// 判断帖子是否收藏过
	private void getwebcheckshoucang(final String url1) {

		HttpUtils httpUtils1 = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("flg", "2");
		if (JaderApplication.user == null) {
			shoucang.setImageResource(R.drawable.shoucang);
		} else{
		params.addBodyParameter("userid", JaderApplication.user.getUserid() + "");
		params.addBodyParameter("postid", all_Bens.getTieziId() + "");
		httpUtils1.send(HttpMethod.POST, url1, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub
						String result = responseInfo.result;
						if (result.equals("是")) {
							scfalg = false;
							shoucang.setImageResource(R.drawable.shoucang1);
						} else {
							scfalg = true;
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub
					}
				});}

	}

	// 取消收藏
	private void getwebquxiao(String url1) {
		HttpUtils httpUtils1 = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("flg", "3");
		if (JaderApplication.user == null) {
			shoucang.setImageResource(R.drawable.shoucang);
			JaderApplication.login(this);
		} else{
		params.addBodyParameter("userid", JaderApplication.user.getUserid() + "");
		params.addBodyParameter("postid", all_Bens.getTieziId() + "");
		httpUtils1.send(HttpMethod.POST, url1, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						TiShi.quxiaoshoucang(HuaTiDetail.this);
					}

					@Override
					public void onFailure(HttpException error, String msg) {

					}
				});}
	}

	// 收藏
	private void getwebshoucang(String url1) {
		HttpUtils httpUtils1 = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("flg", "1");
		if (JaderApplication.user == null) {
			shoucang.setImageResource(R.drawable.shoucang);
			JaderApplication.login(this);
		} else{
		params.addBodyParameter("userid", JaderApplication.user.getUserid() + "");
		params.addBodyParameter("postid", all_Bens.getTieziId() + "");
		httpUtils1.send(HttpMethod.POST, url1, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// TODO Auto-generated method stub
						TiShi.shoucang(HuaTiDetail.this);
						// String result = responseInfo.result;
						// Myappliction.show(PostBeanActivity.this, result);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub
					}
				});

	}
	}
}
